package com.mcarrot.site.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mcarrot.site.model.Affair;
import com.mcarrot.site.service.AffairService;

public class AffairAction extends BaseAction {

	private static final long serialVersionUID = 1588004541018385135L;
	
	private Affair affair;
	private String key;
	private Date startDate;
	private Date endDate;
	private Integer start;
	private Integer limit;
	
	private AffairService affairService;
	
	private final Log log = LogFactory.getLog(getClass());
	
	public String forward(){
		return SUCCESS;
	}
	
	public void list(){
//		log.warn(" ----> params[list()]: \n{\n\tkey:" + key + "\n\tstartDate:" + startDate 
//				+ ", \n\tendDate:" + endDate + ", \n\tstart:" + start + ", \n\tlimit:" + limit + "\n}");
		List<Affair> affairs = new ArrayList<Affair>();
		Integer count = affairService.getCount(key, startDate, endDate);
		affairs = affairService.listAffairs(key, startDate, endDate, start, limit);
		log.info(getClass().getName() + " --> affairs.size[list()]: " + affairs.size() );
		try {
			JSONArray jsonArray = JSONArray.fromObject(affairs);
			JSONObject json = new JSONObject();
			json.accumulate("totalNum", count);
			json.accumulate("list", jsonArray.toString());
			PrintWriter pw = getResponse().getWriter();
//			log.warn(" ----> jsonArry.toString[list()]: " +jsonArray.toString() );
			log.warn(" ----> json.toString[list()]: " +json.toString() );
			pw.write(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
			log.error(getClass().getName() + "--->", e);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(getClass().getName() + "--->", e);
		}
	}
	
	public void create(){
		log.warn(" ----> params[create()]: \n{\n\ttitle:" + affair.getTitle() + "\n\tdescription:" + affair.getDescription() 
				+ ", \n\tcontent:" + affair.getContent() + "\n}");
		affair.setSubDate(new Date());
		Long code = affairService.saveOrUpdate(affair);
		try {
			JSONObject json = new JSONObject();  
			if(code != OP_FAILURE){
				json.accumulate("success", true);
			}else{
				json.accumulate("success", false);
			}
			PrintWriter pw = getResponse().getWriter();
			log.warn(" ----> json.toString[list()]: " +json.toString() );
			pw.write(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
			log.error(getClass().getName() + "--->", e);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(getClass().getName() + "--->", e);
		}
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public AffairService getAffairService() {
		return affairService;
	}

	public void setAffairService(AffairService affairService) {
		this.affairService = affairService;
	}

	public Affair getAffair() {
		return affair;
	}

	public void setAffair(Affair affair) {
		this.affair = affair;
	}
	
}
